package cn.com.pc.content.controller;

import cn.com.pc.aaa.AaaClient;
import cn.com.pc.aaa.Constants;
import cn.com.pc.aaa.model.Identity;
import cn.com.pc.content.domain.Site;
import cn.com.pc.content.domain.authority.SiteOperator;
import cn.com.pc.content.dto.PageForm;
import cn.com.pc.content.dto.PageResult;
import cn.com.pc.content.dto.SiteOperatorDTO;
import cn.com.pc.content.service.SiteOperatorService;
import cn.com.pc.rbac.client.RbacService;
import cn.com.pc.rbac.client.annotation.NeedRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

import static cn.com.pc.content.constant.RbacConstants.ADMIN;

/**
 * 管理后台权限Controller
 */
@RestController
@RequestMapping("/permissions")
public class PermissionController {

    @Autowired
    private AaaClient aaaClient;

    @Autowired
    private SiteOperatorService siteOperatorService;


    /**
     * 查询用户角色及权限
     * @param identity
     * @return
     */
    @GetMapping("/roles")
    @NeedRole(ADMIN)
    public Map<String, Object> listRoles(@RequestAttribute(Constants.AAA_IDENTITY) Identity identity) {
        RbacService rbacService =  aaaClient.getRbacService();
        return new HashMap<>() {{
            put("roles", rbacService.listRolesOf(identity.getId()));
            put("permissions", rbacService.listPermissionsOfOperator(identity.getId()));
        }};
    }


    /**
     * 新增站点数据操作人员
     * @param creation
     * @param site
     * @param identity
     * @return
     */
    @PostMapping("/operators")
    @NeedRole(ADMIN)
    @ResponseStatus(HttpStatus.CREATED)
    public SiteOperatorDTO createSiteOperator(@Valid @RequestBody SiteOperatorDTO creation, @RequestAttribute("site") Site site, @RequestAttribute(Constants.AAA_IDENTITY) Identity identity) {
        SiteOperator toCreate = creation.build(site, identity.getId());
        SiteOperator created = siteOperatorService.createSiteOperator(toCreate);
        return SiteOperatorDTO.from(created);
    }


    /**
     * 删除站点数据操作人员
     * @param siteOperatorId
     * @param site
     * @param identity
     */
    @DeleteMapping("/operators/{id}")
    @NeedRole(ADMIN)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deleteSiteOperator(@PathVariable("id") Long siteOperatorId, @RequestAttribute("site") Site site, @RequestAttribute(Constants.AAA_IDENTITY) Identity identity) {
        siteOperatorService.deleteSiteOperator(siteOperatorId);
    }


    /**
     * 查询站点数据操作人员
     * @param site
     * @param identity
     * @param pageForm
     * @return
     */
    @GetMapping("/operators")
    @NeedRole(ADMIN)
    public PageResult<SiteOperatorDTO> listSiteOperators(@RequestAttribute("site") Site site, @RequestAttribute(Constants.AAA_IDENTITY) Identity identity, PageForm pageForm) {
        return siteOperatorService.listSiteOperators(site, pageForm.getPageNum(), pageForm.getPageSize());
    }
}
